package global.com.loansapplication.ui.actviity.login;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import cn.jpush.android.api.JPushInterface;
import global.com.loansapplication.R;
import global.com.loansapplication.base.BaseActivity;
import global.com.loansapplication.event.NetErrorEvent;
import global.com.loansapplication.ui.actviity.login.biz.RegisterBiz;
import global.com.loansapplication.ui.actviity.login.component.DaggerRegisterComponent;
import global.com.loansapplication.ui.actviity.login.contract.RegisterContract;
import global.com.loansapplication.ui.actviity.login.module.RegisterModule;
import global.com.loansapplication.ui.actviity.login.presenter.RegisterPresenter;
import global.com.loansapplication.util.TimerUtil;
import rx.functions.Action1;

public class RegisterAct extends BaseActivity implements RegisterContract.View {


    @Bind(R.id.register_phone_edit)
    EditText phone;
    @Bind(R.id.register_code_edit)
    EditText code;
    @Bind(R.id.register_get_code)
    TextView getCode;
    @Bind(R.id.register_pwd_edit)
    EditText pwd;
    @Bind(R.id.register_pwd_type)
    CheckBox pwdType;
    @Bind(R.id.register_next)
    TextView next;
    private TimerUtil timerUtil;
    boolean registering = false;
    private String alias;

    @Inject
    RegisterPresenter registerPresenter;

    @Inject
    RegisterBiz registerBiz;

    @Override
    protected void setRoot() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_register);
    }

    @Override
    protected void setup() {
        DaggerRegisterComponent.builder().registerModule(new RegisterModule(this)).build().inject(this);
        registerPresenter.setBiz(registerBiz);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }
    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        timerUtil = new TimerUtil(59000, 1000, getCode);
//        SharedPreferences preferences = getSharedPreferences(Utils.USER, Context.MODE_PRIVATE);
//        alias = preferences.getString(Utils.REGISTERID, "");
//        alias = JPushInterface.getRegistrationID(RegisterAct.this);
//        SharedPreferences.Editor editor = getSharedPreferences(Utils.USER, Context.MODE_PRIVATE).edit();
//        editor.putString(Utils.REGISTERID, alias);
//        editor.apply();
        RxView.clicks(getCode)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        registerPresenter.getCheckCode(phone.getText().toString(), registering);
                    }
                });


        RxView.clicks(pwdType)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        registerPresenter.showPwd(pwdType.isChecked());

                    }
                });
        RxView.clicks(next)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        registerPresenter.toCheck();
                    }
                });

    }


    @Override
    public void countDown() {
        timerUtil.start();
    }

    @Override
    public void updateUI(boolean ishow) {
        if (ishow) {
            pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            pwd.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }

}
